<template>
  <div>
    <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCancel">
      <el-checkbox-group v-model="roleList">
        <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>
      </el-checkbox-group>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { userProfile } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      list: [], //角色列表
      roleList: [] // 当前用户有的角色列表
    }
  },
  created () {
    this.getRoleList()
  },
  methods: {
    async getRoleList () {
      let res = await getRoleList({ page: 1, pagesize: 20 })
      console.log(res)
      this.list = res.data.rows
    },
    async userProfile (id) {
      let res = await userProfile(id)
      console.log(res)
      this.roleList = res.data.roleIds
    },
    async btnOk () {
      await assignRoles({ id: this.userId, roleIds: this.roleList })
      this.$message.success('添加成功')
      this.$emit('update:showRoleDialog', false)
    },
    btnCancel () {
      this.roleList = []
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style></style>
